package cn.houserent.dao;

import cn.houserent.pojo.House;
import org.hibernate.Query;

import java.io.Serializable;
import java.util.List;

public class HouseDao extends BaseDao {

    public House load(Serializable id) {
        return (House)HibernateUtil.currentSession().load(House.class,id);
    }

    public House get(Serializable id) {
        return (House)HibernateUtil.currentSession().get(House.class,id);
    }

    /**
     *  查询标题包括“中关村” 字样
     *
     * @return
     */
    public List<House> getHouseByTitle() {
        return this.currentSession()
                .createQuery("from House where title like ? and floorage > ?")
                .setString(0, "%中关村%").setInteger(1, 80).list();
    }


    public List<House> findHousesByConditions(String hql,House conditions) {
        return this.currentSession().createQuery(hql).setProperties(conditions).list();
    }

    public List<House> findHouseByPage(int pageNo,int pageSize) {
        return currentSession().createQuery("from House order by id").setFirstResult((pageNo - 1) * pageSize) //设置获取结果的起始下标
        .setMaxResults(pageSize)    //设置最大返回结果数
                .list();
    }


    /**
     *  按房屋租金、标题、发布日期以及联系人分页查询房屋信息
     * @param sql
     * @param pageNo
     * @param pageSize
     * @param house
     * @return
     */
    public List<House> findHouseByPageInfo(String sql,int pageNo,int pageSize,House house) {
        Query query = currentSession().createQuery(sql);
        query.setFirstResult((pageNo - 1 ) * pageSize );
        query.setMaxResults(pageSize);
        query.setProperties(house);
        return query.list();
    }


    /**
     *  获取信息总数量
     * @return
     */
    public Long houseCount() {
        String hql = "select count(id) from House where 1 = 1";
        return (Long)currentSession().createQuery(hql).uniqueResult();
    }



}
